from django.db import models
from django.contrib.auth.models import User


class MusicComposition(models.Model):
    """AI音乐创作记录"""
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('processing', '生成中'),
        ('completed', '已完成'),
        ('failed', '生成失败'),
    ]
    
    STYLE_CHOICES = [
        ('classical', '古典音乐'),
        ('pop', '流行音乐'),
        ('rock', '摇滚音乐'),
        ('electronic', '电子音乐'),
        ('jazz', '爵士音乐'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    title = models.CharField(max_length=200, verbose_name='音乐标题')
    description = models.TextField(verbose_name='音乐描述', help_text='描述您想要创作的音乐风格和情感')
    style = models.CharField(max_length=50, choices=STYLE_CHOICES, verbose_name='音乐风格')
    tempo = models.IntegerField(default=120, verbose_name='音乐速度(BPM)')
    duration = models.IntegerField(default=60, verbose_name='时长(秒)')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    audio_url = models.URLField(blank=True, verbose_name='音频链接')
    midi_url = models.URLField(blank=True, verbose_name='MIDI文件链接')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = 'AI音乐创作'
        verbose_name_plural = 'AI音乐创作'
        ordering = ['-created_at']
    
    def __str__(self):
        return f'{self.title} - {self.user.username}'


class AudioSynthesis(models.Model):
    """百度千帆音频合成记录"""
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('processing', '合成中'),
        ('completed', '已完成'),
        ('failed', '合成失败'),
    ]
    
    VOICE_CHOICES = [
        ('0', '女声 - 度小宇'),
        ('1', '男声 - 度小美'),
        ('3', '男声 - 度逍遥'),
        ('4', '男声 - 度丫丫'),
        ('5', '女声 - 度小娇'),
        ('6', '男声 - 度米朵'),
        ('7', '女声 - 度博文'),
        ('8', '女声 - 度小童'),
        ('9', '女声 - 度小萌'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    text_content = models.TextField(verbose_name='合成文本', help_text='要合成为语音的文本内容')
    voice_type = models.CharField(max_length=10, choices=VOICE_CHOICES, default='0', verbose_name='音色类型')
    speed = models.IntegerField(default=5, verbose_name='语速', help_text='取值0-15，默认5')
    pitch = models.IntegerField(default=5, verbose_name='音调', help_text='取值0-15，默认5')
    volume = models.IntegerField(default=5, verbose_name='音量', help_text='取值0-15，默认5')
    audio_format = models.CharField(max_length=10, default='mp3', verbose_name='音频格式')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    audio_file = models.FileField(upload_to='audio_synthesis/', blank=True, verbose_name='音频文件')
    file_size = models.IntegerField(default=0, verbose_name='文件大小(字节)')
    duration = models.FloatField(default=0.0, verbose_name='音频时长(秒)')
    error_message = models.TextField(blank=True, verbose_name='错误信息')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '音频合成'
        verbose_name_plural = '音频合成'
        ordering = ['-created_at']
    
    def __str__(self):
        preview_text = self.text_content[:30] + '...' if len(self.text_content) > 30 else self.text_content
        return f'{self.user.username} - {preview_text}'
